﻿(function ($, frame) {
    "use strict";
	$.extend(frame, {
		exportExcel: function (url, postData) {
			frame.ajax({
				url: url,
				type: "post",
				data: postData,
				success: function (obj) {  
					if (obj.success) { 
						if (obj.data == 0) {
							frame.msgWarning(obj.message); 
						} else{
							window.location.href = "ExportFile?filePath=" + obj.data;
						}
					} 
					else {
						frame.msgError(obj.message);
					}
				},
				beforeSend: function (xhr) {
					frame.showLoading("正在导出数据");
				}
			});
		},
		getIds: function (row) {
			var ids = '';
			$.each(row, function (i, obj) {
				if (i == 0) {
					ids = obj.Id;
				}
				else {
					ids += "," + obj.Id;
				}
			});
			return ids;
		},
		checkRowEdit: function (row) { 
			if (row.length == 0) {
				frame.msgError("您没有选择任何行！");
			} else if (row.length > 1) {
				frame.msgError("您的选择大于1行！");
			} else if (row.length == 1) {
				return true;
			}
			return false;
		},
		checkRowDelete: function (row) {
			if (row.length == 0) {
				frame.msgError("您没有选择任何行！");
			} else if (row.length > 0) {
				return true;
			}
			return false;
		},
		checkRowSelect: function (row) {
			if (row.length == 0) {
				frame.msgError("您没有选择任何行！");
			} else if (row.length > 0) {
				return true;
			}
			return false;
		},
		trimStart: function (rawStr, c) {
			if (c == null || c == '') {
				var str = rawStr.replace(/^s*/, '');
				return str;
			}
			else {
				var rg = new RegExp('^' + c + '*');
				var str = rawStr.replace(rg, '');
				return str;
			}
		},
		trimEnd: function (rawStr, c) {
			if (c == null || c == "") {
				var rg = /s/;
				var i = rawStr.length;
				while (rg.test(rawStr.charAt(--i)));
				return rawStr.slice(0, i + 1);
			}
			else {
				var rg = new RegExp(c);
				var i = rawStr.length;
				while (rg.test(rawStr.charAt(--i)));
				return rawStr.slice(0, i + 1);
			}
		},
		toString: function (value) {
			if (value == null) {
				return '';
			}
			return value.toString();
		},
		getGuid: function () {
			var guid = "";
			for (var i = 1; i <= 32; i++) {
				var n = Math.floor(Math.random() * 16.0).toString(16);
				guid += n;
				if ((i == 8) || (i == 12) || (i == 16) || (i == 20)) guid += "-";
			}
			return guid;
		},
        isMobile: function () { 
			if (/mobile|android|iphone|ipad|ipod|opera mini|blackberry|webos|iemobile|windows ce|windows phone/i.test(navigator.userAgent)) {
				return true;
			} else {
				return false;
			}
        }, 
		formatDate: function (v, format) {
			if (!v) return "";
			var d = v;
			if (typeof v === 'string') {
				if (v.indexOf("/Date(") > -1)
					d = new Date(parseInt(v.replace("/Date(", "").replace(")/", ""), 10));
				else
					d = new Date(Date.parse(v.replace(/-/g, "/").replace("T", " ").split(".")[0]));
			}
			var o = {
				"M+": d.getMonth() + 1,  //month
				"d+": d.getDate(),       //day
				"H+": d.getHours(),      //hour
				"m+": d.getMinutes(),    //minute
				"s+": d.getSeconds(),    //second
				"q+": Math.floor((d.getMonth() + 3) / 3),  //quarter
				"S": d.getMilliseconds() //millisecondjsonca4
			};
			if (/(y+)/.test(format)) {
				format = format.replace(RegExp.$1, (d.getFullYear() + "").substr(4 - RegExp.$1.length));
			}
			for (var k in o) {
				if (new RegExp("(" + k + ")").test(format)) {
					format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
				}
			}
			return format;
		},
		formatCurrency: function (number, currency) {
			if (typeof number !== 'number' || isNaN(number)) {
				number = 0;
			}  
			var options = {
				minimumFractionDigits: 2,
				maximumFractionDigits: 2,
				style: 'currency',
				currency: currency
			};
			switch (currency) {
				case "CNY":
					return number.toLocaleString('zh-CN', options);
					break;
				case "USD":
					return number.toLocaleString('en-US', options);
					break;
            }
			
		}

    });
})(window.jQuery, window.frame);